/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import database.ProductDAO;
import java.util.List;
import javax.servlet.annotation.*;
import model.Category;
import model.Product;
import org.apache.struts2.convention.annotation.*;

/**
 *
 * @author Thai Ha Cong
 */
//@Namespace("/Category/{idCategory}")
@Namespace("/")
public class CategoryAction extends ActionSupport {

    private Integer idCategory;
    private String cname;
    private Category category;
    private ProductDAO productDAO = new ProductDAO();
    private List<Product> productsResultList;
    private int currentPage;
    private int totalPage;
    private int itemsPerPage = 10;

    public int getItemsPerPage() {
        return itemsPerPage;
    }

    public void setItemsPerPage(int itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public CategoryAction() {
        currentPage = 1;
        totalPage = 1;
    }

    public Integer getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Product> getProductsResultList() {
        return productsResultList;
    }

    public void setProductsResultList(List<Product> productsResultList) {
        this.productsResultList = productsResultList;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    @Action(value = "category/*/*", results = {
        @Result(name = "success", location = "/category.jsp"),
        @Result(name = "error", location = "/index.jsp")},
            params = {"idCategory", "{1}", "cname", "{2}"})
    public String execute() throws Exception {
        int totalItems = productDAO.countProductsFromCategoryID(idCategory);
        category = productDAO.getCategoryFromId(idCategory);
//        System.out.println(currentPage);
        if ((cname.replaceAll("-", " ")).equals(category.getName().replaceAll("-", " "))) {
            if (totalItems == 0) {
                totalPage = currentPage = 0;
                addActionMessage("No Items Found");

                return SUCCESS;
            } else {
                totalPage = totalItems / itemsPerPage + (totalItems % itemsPerPage == 0 ? 0 : 1);
                int offset = (currentPage - 1) * itemsPerPage;

                productsResultList = productDAO.getProductsFromCategoryID(idCategory, offset, itemsPerPage);

                return SUCCESS;
            }
        } else {
            return ERROR;
        }
    }

    @Action(value = "category", results = {
        @Result(name = "success", location = "/home.jsp")
    })
    @HttpMethodConstraint("POST")
    public String category() throws Exception {
        productsResultList = productDAO.getProductsFromCategoryID(idCategory);

        return SUCCESS;
    }
}